/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bttk.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bttk.entity.TUserCrowdProject;
import com.thinkgem.jeesite.modules.bttk.service.TUserCrowdProjectService;

/**
 * 众筹项目Controller
 * @author soyatec
 * @version 2016-11-02
 */
@Controller
@RequestMapping(value = "${adminPath}/bttk/tUserCrowdProject")
public class TUserCrowdProjectController extends BaseController {

	@Autowired
	private TUserCrowdProjectService tUserCrowdProjectService;
	
	@ModelAttribute("tUserCrowdProject")
	public TUserCrowdProject get(@RequestParam(required=false) String id) {
		TUserCrowdProject tUserCrowdProject = null;
		if (StringUtils.isNotBlank(id)){
			tUserCrowdProject = tUserCrowdProjectService.get(id);
		}
		if (tUserCrowdProject == null){
			tUserCrowdProject = new TUserCrowdProject();
		}
		return tUserCrowdProject;
	}
	
	@RequiresPermissions("bttk:tUserCrowdProject:view")
	@RequestMapping(value = {"list", ""})
	public String list(TUserCrowdProject tUserCrowdProject, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TUserCrowdProject> page = tUserCrowdProjectService.findPage(new Page<TUserCrowdProject>(request, response), tUserCrowdProject); 
		model.addAttribute("page", page);
		return "modules/bttk/tUserCrowdProjectList";
	}

	@RequiresPermissions("bttk:tUserCrowdProject:view")
	@RequestMapping(value = "form")
	public String form(TUserCrowdProject tUserCrowdProject, Model model) {
		tUserCrowdProject=tUserCrowdProjectService.get(tUserCrowdProject.getId());
		model.addAttribute("tUserCrowdProject", tUserCrowdProject);
		return "modules/bttk/tUserCrowdProjectForm";
	}
     
	@RequiresPermissions("bttk:tUserCrowdProject:view")
	@RequestMapping(value = "add")
	public String add() {
	
		return "modules/bttk/tUserCrowdProjectAdd";
	}
	
	@RequiresPermissions("bttk:tUserCrowdProject:edit")
	@RequestMapping(value = "save")
	public String save(TUserCrowdProject tUserCrowdProject, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tUserCrowdProject)){
			return form(tUserCrowdProject, model);
		}
		tUserCrowdProjectService.save(tUserCrowdProject);
		addMessage(redirectAttributes, "保存众筹项目成功");
		return "redirect:"+Global.getAdminPath()+"/bttk/tUserCrowdProject/?repage";
	}
	
	@RequiresPermissions("bttk:tUserCrowdProject:edit")
	@RequestMapping(value = "delete")
	public String delete(TUserCrowdProject tUserCrowdProject, RedirectAttributes redirectAttributes) {
		tUserCrowdProjectService.delete(tUserCrowdProject);
		addMessage(redirectAttributes, "结束众筹项目成功");
		return "redirect:"+Global.getAdminPath()+"/bttk/tUserCrowdProject/?repage";
	}

}